package com.blood.core.config.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/15 19:35
 * @description Swagger自动配置
 */
@Slf4j
@EnableKnife4j
@Configuration
public class SwaggerAutoConfiguration {
    @Value("${basic.support.bean.swagger.contact-name}")
    private String contactName;
    @Value("${basic.support.bean.swagger.contact-url}")
    private String contactUrl;
    @Value("${basic.support.bean.swagger.contact-email}")
    private String contactEmail;
    @Value("${basic.support.bean.swagger.title}")
    private String title;
    @Value("${basic.support.bean.swagger.description}")
    private String description;
    @Value("${basic.support.bean.swagger.version}")
    private String version;
    @Value("${basic.support.bean.swagger.base-package}")
    private String basePackage;

    @Bean
    public Docket createRestApi() {
        log.info("knife4j自动配置 ----->");
        return new Docket(DocumentationType.OAS_30).pathMapping("/")
                // 将api的元信息设置为包含在json ResourceListing响应中。
                .apiInfo(apiInfo())
                .groupName("smart")
                // 选择哪些接口作为swagger的doc发布
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()

//                // 支持的通讯协议集合
//                .protocols(newHashSet("https", "http"))

                // 授权信息设置，必要的header token等认证信息
                .securitySchemes(securitySchemes())

                // 授权信息全局应用
                .securityContexts(securityContexts());

    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact(contactName, contactUrl, contactEmail);
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .contact(contact)
                .version(version)
                .build();
    }

    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes() {
        ApiKey token = new ApiKey("token", "token", In.HEADER.toValue());
        ApiKey platform = new ApiKey("platform", "platform", In.HEADER.toValue());
        List<SecurityScheme> singletonList = new ArrayList<>();
        singletonList.add(token);
        singletonList.add(platform);
        return singletonList;
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityReference> securityReferences = new ArrayList<>();
        SecurityReference token = new SecurityReference("token", new AuthorizationScope[]{new AuthorizationScope("global", "")});
        SecurityReference platform = new SecurityReference("platform", new AuthorizationScope[]{new AuthorizationScope("global", "")});
        securityReferences.add(token);
        securityReferences.add(platform);
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(securityReferences)
                        .build()
        );
    }

    @SafeVarargs
    private final <T> Set<T> newHashSet(T... ts) {
        if (ts.length > 0) {
            return new LinkedHashSet<>(Arrays.asList(ts));
        }
        return null;
    }

}
